(function ($) {
    // iPhone x 适配
    var resizeOnce = false;
    var changeScaleMode = function(){
        var scaleMode;
        switch(Laya.stage.screenMode){
            case 'horizontal':
                scaleMode = Laya.Browser.width / Laya.Browser.height > 1334 / 750 ? Laya.Stage.SCALE_SHOWALL : Laya.Stage.SCALE_FIXED_WIDTH;
                break;
            case 'vertical':
                scaleMode = Laya.Browser.width / Laya.Browser.height < 750 / 1334 ? Laya.Stage.SCALE_SHOWALL : Laya.Stage.SCALE_FIXED_HEIGHT;
                break;
            default:
                scaleMode = Laya.Stage.SCALE_SHOWALL;
                break;
        }
        Laya.stage.scaleMode = scaleMode;
        Laya.stage.setScreenSize(Laya.Browser.clientWidth * Laya.Browser.pixelRatio, Laya.Browser.clientHeight * Laya.Browser.pixelRatio);
    }
    var resizeListen = function(){
        var timeoutResize;
        var resizeFunc = function(){
            clearTimeout(timeoutResize);
            timeoutResize = setTimeout(function(){
                changeScaleMode();
            }, 200);
        }
        window.addEventListener('resize', resizeFunc);
    }

    function Game() {
        this.director = null;
        this.io = null;
        this.viewer = null;

        this.init();
    }
    var _proto = Game.prototype;

    _proto.init = function () {
        if(utils.getUrlParam("debug_status") != "1"){
            GAME_DEBUG_STATUS = false;
            window.console && (console.log = console.trace = console.error = function () {});
        }else{
            GAME_DEBUG_STATUS = true;
        }
        
        this.viewer = new Libs.Viewer();
        this.io = new Libs.IO();
    }

    _proto.run = function (width, height) {
        Laya.init(width, height, Laya.WebGL);

        Laya.stage.once('resize', this, function(){
            if(resizeOnce){
                return;
            }
            resizeOnce = true;
            
            changeScaleMode();
            resizeListen();

            this.director = new Libs.Director();
            Laya.stage.addChild(this.director);

            this.onStart && this.onStart();

            Laya.stage.on(Laya.Event.RESIZE, this, function () {
                this.director.onResize(Laya.stage.height);
            });
            Laya.stage.event(Laya.Event.RESIZE);
        });
    }

    $.ppt = new Game();
})(window);